
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
  <meta charset="utf-8">
  <title>ksnowlv</title>
  <meta name="author" content="律威">

  
  <meta name="description" content="学习python调试可参考官方文档的pdb. 一.在python中,在调试模块添加pdb调试支持. 1
2
3 import pdb; pdb.set_trace() 不然，会遇见类似如下的错误: (Pdb) n
&gt; /Users/ksnowlv/Documents/lvwei &hellip;">
  

  <!-- http://t.co/dKP3o1e -->
  <meta name="HandheldFriendly" content="True">
  <meta name="MobileOptimized" content="320">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  
  <link rel="canonical" href="http://ksnowlv.github.io/blog/page/11">
  <link href="/favicon.png" rel="icon">
  <link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
  <link href="/atom.xml" rel="alternate" title="ksnowlv" type="application/atom+xml">
  <script src="/javascripts/modernizr-2.0.js"></script>
  <script src="//ajax.useso.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script>!window.jQuery && document.write(unescape('%3Cscript src="./javascripts/lib/jquery.min.js"%3E%3C/script%3E'))</script>
  <script src="/javascripts/octopress.js" type="text/javascript"></script>
  <!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
<link href="http://fonts.useso.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
<link href="http://fonts.useso.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">

  
  <script type="text/javascript">
    var _gaq = _gaq || [];
     _gaq.push(['_setAccount', 'UA-39546615-2']);
    // _gaq.push(['_trackPageview']);

    // (function() {
    //   var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    //   ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    //   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    // })();

    _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src='http://droidyue-tools.qiniudn.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
  </script>


</head>

<body   >
  <header role="banner"><hgroup>
  <h1><a href="/">ksnowlv</a></h1>
  
    <h2>回顾过去,总结以往;立足现在,铭记当下;技术为主,笔记而已.</h2>
  
</hgroup>

</header>
  <nav role="navigation"><ul class="subscription" data-subscription="rss">
  <li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
  
</ul>
  
<form action="http://google.com/search" method="get">
  <fieldset role="search">
    <input type="hidden" name="q" value="site:ksnowlv.github.io" />
    <input class="search" type="text" name="q" results="0" placeholder="Search"/>
  </fieldset>
</form>
  
<ul class="main-navigation">
  <li><a href="/">博客首页</a></li>
  <li><a href="/blog/archives">文章列表</a></li>
  <!-- <li><a href="/about-me">关于我</a></li>
 -->
</ul>

</nav>
  <div id="main">
    <div id="content">
      <div class="blog-index">
  
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/08/09/python-diao-shi-zhi-pdb/">Python调试之pdb</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-08-09T14:40:00+08:00" pubdate data-updated="true">Aug 9<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/08/09/python-diao-shi-zhi-pdb/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h4>学习python调试可参考官方文档的<a href="https://docs.python.org/2/library/pdb.html">pdb</a>.</h4>

<h4>一.在python中,在调试模块添加pdb调试支持.</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'>    <span class="kn">import</span> <span class="nn">pdb</span><span class="p">;</span>
</span><span class='line'>    <span class="n">pdb</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span>
</span><span class='line'>
</span></code></pre></td></tr></table></div></figure>


<p></p>

<h4>不然，会遇见类似如下的错误:</h4>

<pre><code>(Pdb) n
&gt; /Users/ksnowlv/Documents/lvwei projects/python/thread_queue.py(38)&lt;module&gt;()
-&gt; nameList = ["One", "Two", "Three", "Four", "Five"]
(Pdb) a
(Pdb) p namelist
*** NameError: NameError("name 'namelist' is not defined",)
</code></pre>

<h4>二.一些常用命令：</h4>

<h4>查看帮助</h4>

<pre><code>help
(Pdb) 
Documented commands (type help &lt;topic&gt;):
========================================
EOF    bt         cont      enable  jump  pp       run      unt   
a      c          continue  exit    l     q        s        until 
alias  cl         d         h       list  quit     step     up    
args   clear      debug     help    n     r        tbreak   w     
b      commands   disable   ignore  next  restart  u        whatis
break  condition  down      j       p     return   unalias  where 
</code></pre>

<p>解释如下:</p>

<pre><code>b/break  &lt;linenumber&gt;   #断点设置在本py的第linenumber行
b/break  &lt;functionname&gt; #断点设置在本py的functionname
b/break                 #列举当前所有的断点
cl/clear                #清除所有断点
cl/clear  &lt;number&gt;      #清除第number个断点 
n/next        #单步运行到下一步
s/step        #进入到函数内部
c/continue    #继续运行
p             #查看当前变量值
l/list        #查看运行到某处代码
a             #查看全部栈内变量
w             #列出目前call stack 中的所在层
d             #在call stack中往下移一层
disable       #取消所有断点的功能,但仍然保留这些断点 
enable        #恢复断点的功能  
j/jump        #跳转到某行执行 
</code></pre>

<h4>代码示例如下:</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="c">#!/usr/bin/python</span>
</span><span class='line'>
</span><span class='line'><span class="kn">import</span> <span class="nn">Queue</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">threading</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">time</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">pdb</span><span class="p">;</span>
</span><span class='line'><span class="n">pdb</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'><span class="n">exitFlag</span> <span class="o">=</span> <span class="mi">0</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">QThread</span> <span class="p">(</span><span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">):</span>
</span><span class='line'>
</span><span class='line'>    <span class="s">&quot;QThread class&quot;</span>
</span><span class='line'>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">threadID</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">queue</span><span class="p">):</span>
</span><span class='line'>        <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
</span><span class='line'>        <span class="bp">self</span><span class="o">.</span><span class="n">threadID</span> <span class="o">=</span> <span class="n">threadID</span>
</span><span class='line'>        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
</span><span class='line'>        <span class="bp">self</span><span class="o">.</span><span class="n">queue</span> <span class="o">=</span> <span class="n">queue</span>
</span><span class='line'>
</span><span class='line'>    <span class="c">#线程函数</span>
</span><span class='line'>    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class='line'>        <span class="k">print</span> <span class="s">&quot;Starting &quot;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
</span><span class='line'>        <span class="n">process_data</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">queue</span><span class="p">)</span>
</span><span class='line'>        <span class="k">print</span> <span class="s">&quot;Exiting &quot;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
</span><span class='line'>
</span><span class='line'><span class="c">#处理数据</span>
</span><span class='line'><span class="k">def</span> <span class="nf">process_data</span><span class="p">(</span><span class="n">threadName</span><span class="p">,</span> <span class="n">q</span><span class="p">):</span>
</span><span class='line'>    <span class="k">while</span> <span class="ow">not</span> <span class="n">exitFlag</span><span class="p">:</span>
</span><span class='line'>        <span class="n">queueLock</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
</span><span class='line'>        <span class="k">if</span> <span class="ow">not</span> <span class="n">workQueue</span><span class="o">.</span><span class="n">empty</span><span class="p">():</span>
</span><span class='line'>            <span class="n">data</span> <span class="o">=</span> <span class="n">q</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
</span><span class='line'>            <span class="n">queueLock</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
</span><span class='line'>            <span class="k">print</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s"> processing </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">threadName</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>
</span><span class='line'>        <span class="k">else</span><span class="p">:</span>
</span><span class='line'>            <span class="n">queueLock</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
</span><span class='line'>        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="n">threadList</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;ksnow&quot;</span><span class="p">,</span> <span class="s">&quot;kair&quot;</span><span class="p">,</span> <span class="s">&quot;baby&quot;</span><span class="p">]</span>
</span><span class='line'><span class="n">nameList</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;One&quot;</span><span class="p">,</span> <span class="s">&quot;Two&quot;</span><span class="p">,</span> <span class="s">&quot;Three&quot;</span><span class="p">,</span> <span class="s">&quot;Four&quot;</span><span class="p">,</span> <span class="s">&quot;Five&quot;</span><span class="p">]</span>
</span><span class='line'><span class="n">queueLock</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Lock</span><span class="p">()</span>
</span><span class='line'><span class="n">workQueue</span> <span class="o">=</span> <span class="n">Queue</span><span class="o">.</span><span class="n">Queue</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
</span><span class='line'><span class="n">threads</span> <span class="o">=</span> <span class="p">[]</span>
</span><span class='line'><span class="n">threadID</span> <span class="o">=</span> <span class="mi">1</span>
</span><span class='line'>
</span><span class='line'><span class="c"># 创建新线程</span>
</span><span class='line'><span class="k">for</span> <span class="n">tName</span> <span class="ow">in</span> <span class="n">threadList</span><span class="p">:</span>
</span><span class='line'>    <span class="n">thread</span> <span class="o">=</span> <span class="n">QThread</span><span class="p">(</span><span class="n">threadID</span><span class="p">,</span> <span class="n">tName</span><span class="p">,</span> <span class="n">workQueue</span><span class="p">)</span>
</span><span class='line'>    <span class="n">thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
</span><span class='line'>    <span class="n">threads</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">thread</span><span class="p">)</span>
</span><span class='line'>    <span class="n">threadID</span> <span class="o">+=</span> <span class="mi">1</span>
</span><span class='line'>
</span><span class='line'><span class="c"># 填充队列</span>
</span><span class='line'><span class="n">queueLock</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
</span><span class='line'><span class="k">for</span> <span class="n">word</span> <span class="ow">in</span> <span class="n">nameList</span><span class="p">:</span>
</span><span class='line'>    <span class="n">workQueue</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">word</span><span class="p">)</span>
</span><span class='line'><span class="n">queueLock</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'><span class="c"># 等待队列清空</span>
</span><span class='line'><span class="k">while</span> <span class="ow">not</span> <span class="n">workQueue</span><span class="o">.</span><span class="n">empty</span><span class="p">():</span>
</span><span class='line'>    <span class="k">pass</span>
</span><span class='line'>
</span><span class='line'><span class="c"># 通知线程是时候退出</span>
</span><span class='line'><span class="n">exitFlag</span> <span class="o">=</span> <span class="mi">1</span>
</span><span class='line'>
</span><span class='line'><span class="c"># 等待所有线程完成</span>
</span><span class='line'><span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">threads</span><span class="p">:</span>
</span><span class='line'>    <span class="n">t</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
</span><span class='line'><span class="k">print</span> <span class="s">&quot;Exiting Main Thread&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<h4>调试示例如下：</h4>

<pre><code>ksnowlv@ksnowlvdeMacBook-Pro~/Documents/lvwei projects/python$python thread_queue.py 
&gt; /Users/ksnowlv/Documents/lvwei projects/python/thread_queue.py(9)&lt;module&gt;()
-&gt; exitFlag = 0
(Pdb) b process_data
Breakpoint 1 at /Users/ksnowlv/Documents/lvwei projects/python/thread_queue.py:27
(Pdb) b 45
Breakpoint 2 at /Users/ksnowlv/Documents/lvwei projects/python/thread_queue.py:45
(Pdb) b 33
Breakpoint 3 at /Users/ksnowlv/Documents/lvwei projects/python/thread_queue.py:33
(Pdb) b
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at /Users/ksnowlv/Documents/lvwei projects/python/thread_queue.py:27
2   breakpoint   keep yes   at /Users/ksnowlv/Documents/lvwei projects/python/thread_queue.py:45
3   breakpoint   keep yes   at /Users/ksnowlv/Documents/lvwei projects/python/thread_queue.py:33
(Pdb) cl 3
Deleted breakpoint 3
(Pdb) b
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at /Users/ksnowlv/Documents/lvwei projects/python/thread_queue.py:27
2   breakpoint   keep yes   at /Users/ksnowlv/Documents/lvwei projects/python/thread_queue.py:45
(Pdb) b 33
Breakpoint 4 at /Users/ksnowlv/Documents/lvwei projects/python/thread_queue.py:33
(Pdb) c
&gt; /Users/ksnowlv/Documents/lvwei projects/python/thread_queue.py(45)&lt;module&gt;()
-&gt; threadID = 1
(Pdb) p threadList
['ksnow', 'kair', 'baby']
(Pdb) pp nameList
['One', 'Two', 'Three', 'Four', 'Five']
(Pdb) n
&gt; /Users/ksnowlv/Documents/lvwei projects/python/thread_queue.py(48)&lt;module&gt;()
-&gt; for tName in threadList:
(Pdb) n
&gt; /Users/ksnowlv/Documents/lvwei projects/python/thread_queue.py(49)&lt;module&gt;()
-&gt; thread = QThread(threadID, tName, workQueue)
(Pdb) s
--Call--
&gt; /Users/ksnowlv/Documents/lvwei projects/python/thread_queue.py(14)__init__()
-&gt; def __init__(self, threadID, name, queue):
(Pdb) s
&gt; /Users/ksnowlv/Documents/lvwei projects/python/thread_queue.py(15)__init__()
-&gt; threading.Thread.__init__(self)
(Pdb) s
--Call--
&gt; /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py(654)__init__()
-&gt; def __init__(self, group=None, target=None, name=None,
(Pdb) c
Starting ksnow
Starting kair
Starting baby
ksnow processing One
baby processing Two
 kair processing Three
ksnow processing Four
kair processing Five
Exiting baby
Exiting ksnow
Exiting kair
Exiting Main Thread
Exception AttributeError: "'NoneType' object has no attribute 'path'" in &lt;function _remove at 0x10fb59b18&gt; ignored
ksnowlv@ksnowlvdeMacBook-Pro~/Documents/lvwei projects/python$
</code></pre>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/08/09/qi-yue-fen-ipad-tao-bao-ke-hu-duan-you-hua-zong-jie/">七月份iPad客户端优化总结</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-08-09T00:11:00+08:00" pubdate data-updated="true">Aug 9<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/08/09/qi-yue-fen-ipad-tao-bao-ke-hu-duan-you-hua-zong-jie/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h3>主要内容包含三方面</h3>

<pre><code>一.性能优化总览. 
二.性能优化阶段详情. 
三.归档.
</code></pre>

<h3>一.性能优化总览</h3>

<h4>1.第一阶段：性能标准/监控/栅栏。      &mdash;&mdash;-已完成，细节可能会有微调。</h4>

<h4>2.第二阶段：目前处于性能优化之内存优化方面</h4>

<pre><code>a.关于图片方面优化                      -----正在进行中。
b.主线程大扫大部分模块已完成.          -----个别模块在扫尾中。
c.测试同学3.1版本内存占用”摸底”     -----正在进行中。
</code></pre>

<h4>3.第三阶段：</h4>

<pre><code>     a.专题优化工具使用.                -----部分完成
     b.专题优化方法总结.                -----部分完成
</code></pre>

<h4>专题优化穿插事项</h4>

<pre><code>优化前后性能对比，由测试同学在相关主题完结后，给出相关测试报告。
</code></pre>

<p><img src="/images/post/2014-08-09-qi-yue-fen-ipad-tao-bao-ke-hu-duan-you-hua-zong-jie/performance-optimization-overview.png" alt="image" /></p>

<h3>二.性能优化阶段详情</h3>

<h4>1.第一阶段详情</h4>

<p><img src="/images/post/2014-08-09-qi-yue-fen-ipad-tao-bao-ke-hu-duan-you-hua-zong-jie/xingnengyouhuajiduan-yi-xingneng-zhibiao-jiankong.png" alt="image" /></p>

<h4>2.第二阶段详情</h4>

<p><img src="/images/post/2014-08-09-qi-yue-fen-ipad-tao-bao-ke-hu-duan-you-hua-zong-jie/xingnengyouhuajiduan-er-zhuantiyouhua.png" alt="image" /></p>

<h4>3.第三阶段详情</h4>

<p><img src="/images/post/2014-08-09-qi-yue-fen-ipad-tao-bao-ke-hu-duan-you-hua-zong-jie/xingnengyouhuajiduan-shan-chendian-jilei.png" alt="image" /></p>

<h3>三.归档</h3>

<pre><code>文档相关链接见...
</code></pre>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/08/08/python-xian-cheng-zhi-dui-lie-queue/">python线程之队列Queue</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-08-08T23:44:00+08:00" pubdate data-updated="true">Aug 8<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/08/08/python-xian-cheng-zhi-dui-lie-queue/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><p>Python的Queue模块中提供了同步的、线程安全的队列类.</p>

<pre><code>1.FIFO（先入先出)队列Queue，
2.LIFO（后入先出）队列LifoQueue，
3.优先级队列PriorityQueue。
</code></pre>

<p>这些队列都实现了锁原语，能够在多线程中直接使用。可以使用队列来实现线程间的同步。</p>

<p>代码示例如下:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="c">#!/usr/bin/python</span>
</span><span class='line'>
</span><span class='line'><span class="kn">import</span> <span class="nn">Queue</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">threading</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">time</span>
</span><span class='line'>
</span><span class='line'><span class="n">exitFlag</span> <span class="o">=</span> <span class="mi">0</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">QThread</span> <span class="p">(</span><span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">):</span>
</span><span class='line'>
</span><span class='line'>    <span class="s">&quot;QThread class&quot;</span>
</span><span class='line'>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">threadID</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">queue</span><span class="p">):</span>
</span><span class='line'>        <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
</span><span class='line'>        <span class="bp">self</span><span class="o">.</span><span class="n">threadID</span> <span class="o">=</span> <span class="n">threadID</span>
</span><span class='line'>        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
</span><span class='line'>        <span class="bp">self</span><span class="o">.</span><span class="n">queue</span> <span class="o">=</span> <span class="n">queue</span>
</span><span class='line'>
</span><span class='line'>    <span class="c">#线程函数</span>
</span><span class='line'>    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class='line'>        <span class="k">print</span> <span class="s">&quot;Starting &quot;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
</span><span class='line'>        <span class="n">process_data</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">queue</span><span class="p">)</span>
</span><span class='line'>        <span class="k">print</span> <span class="s">&quot;Exiting &quot;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
</span><span class='line'>
</span><span class='line'><span class="c">#处理数据</span>
</span><span class='line'><span class="k">def</span> <span class="nf">process_data</span><span class="p">(</span><span class="n">threadName</span><span class="p">,</span> <span class="n">q</span><span class="p">):</span>
</span><span class='line'>    <span class="k">while</span> <span class="ow">not</span> <span class="n">exitFlag</span><span class="p">:</span>
</span><span class='line'>        <span class="n">queueLock</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
</span><span class='line'>        <span class="k">if</span> <span class="ow">not</span> <span class="n">workQueue</span><span class="o">.</span><span class="n">empty</span><span class="p">():</span>
</span><span class='line'>            <span class="n">data</span> <span class="o">=</span> <span class="n">q</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
</span><span class='line'>            <span class="n">queueLock</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
</span><span class='line'>            <span class="k">print</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s"> processing </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">threadName</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>
</span><span class='line'>        <span class="k">else</span><span class="p">:</span>
</span><span class='line'>            <span class="n">queueLock</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
</span><span class='line'>        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="n">threadList</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;ksnow&quot;</span><span class="p">,</span> <span class="s">&quot;kair&quot;</span><span class="p">,</span> <span class="s">&quot;baby&quot;</span><span class="p">]</span>
</span><span class='line'><span class="n">nameList</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;One&quot;</span><span class="p">,</span> <span class="s">&quot;Two&quot;</span><span class="p">,</span> <span class="s">&quot;Three&quot;</span><span class="p">,</span> <span class="s">&quot;Four&quot;</span><span class="p">,</span> <span class="s">&quot;Five&quot;</span><span class="p">]</span>
</span><span class='line'><span class="n">queueLock</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Lock</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'><span class="c">#创建FIFO（先入先出)队列Queue</span>
</span><span class='line'><span class="n">workQueue</span> <span class="o">=</span> <span class="n">Queue</span><span class="o">.</span><span class="n">Queue</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
</span><span class='line'><span class="n">threads</span> <span class="o">=</span> <span class="p">[]</span>
</span><span class='line'><span class="n">threadID</span> <span class="o">=</span> <span class="mi">1</span>
</span><span class='line'>
</span><span class='line'><span class="c"># 创建新线程</span>
</span><span class='line'><span class="k">for</span> <span class="n">tName</span> <span class="ow">in</span> <span class="n">threadList</span><span class="p">:</span>
</span><span class='line'>    <span class="n">thread</span> <span class="o">=</span> <span class="n">QThread</span><span class="p">(</span><span class="n">threadID</span><span class="p">,</span> <span class="n">tName</span><span class="p">,</span> <span class="n">workQueue</span><span class="p">)</span>
</span><span class='line'>    <span class="n">thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
</span><span class='line'>    <span class="n">threads</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">thread</span><span class="p">)</span>
</span><span class='line'>    <span class="n">threadID</span> <span class="o">+=</span> <span class="mi">1</span>
</span><span class='line'>
</span><span class='line'><span class="c"># 填充队列</span>
</span><span class='line'><span class="n">queueLock</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
</span><span class='line'><span class="k">for</span> <span class="n">word</span> <span class="ow">in</span> <span class="n">nameList</span><span class="p">:</span>
</span><span class='line'>    <span class="n">workQueue</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">word</span><span class="p">)</span>
</span><span class='line'><span class="n">queueLock</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'><span class="c"># 等待队列清空</span>
</span><span class='line'><span class="k">while</span> <span class="ow">not</span> <span class="n">workQueue</span><span class="o">.</span><span class="n">empty</span><span class="p">():</span>
</span><span class='line'>    <span class="k">pass</span>
</span><span class='line'>
</span><span class='line'><span class="c"># 通知线程是时候退出</span>
</span><span class='line'><span class="n">exitFlag</span> <span class="o">=</span> <span class="mi">1</span>
</span><span class='line'>
</span><span class='line'><span class="c"># 等待所有线程完成</span>
</span><span class='line'><span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">threads</span><span class="p">:</span>
</span><span class='line'>    <span class="n">t</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
</span><span class='line'><span class="k">print</span> <span class="s">&quot;Exiting Main Thread&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>运行结果如下:</p>

<pre><code>ksnowlv@ksnowlvdeMacBook-Pro~/Documents/lvwei projects/python$python thread_queue.py
Starting ksnow
Starting kair
Starting baby
kair processing One
ksnow processing Two
baby processing Three
ksnow processing Four
kair processing Five
Exiting baby
Exiting ksnow
Exiting kair
Exiting Main Thread
</code></pre>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/08/08/python-xian-cheng-zhi-tong-bu/">Python线程之同步</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-08-08T23:05:00+08:00" pubdate data-updated="true">Aug 8<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/08/08/python-xian-cheng-zhi-tong-bu/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><p>如果多个线程共同对某个数据修改访问，为了保证数据的正确性，需要对多个线程进行同步。</p>

<p>使用Thread对象的Lock和Rlock可以实现简单的线程同步，</p>

<pre><code>acquire:请求锁,成功则返回true.
release:释放锁.
</code></pre>

<p>对于需要每次只允许一个线程操作的数据，可以使用上述锁来实现互斥访问。</p>

<p>代码示例如下:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="c">#!/usr/bin/python</span>
</span><span class='line'>
</span><span class='line'><span class="kn">import</span> <span class="nn">threading</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">time</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">QThread</span> <span class="p">(</span><span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">):</span>
</span><span class='line'>
</span><span class='line'>    <span class="sd">&quot;&quot;&quot; QThread class&quot;&quot;&quot;</span>
</span><span class='line'>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">threadID</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">counter</span><span class="p">):</span>
</span><span class='line'>        <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
</span><span class='line'>        <span class="bp">self</span><span class="o">.</span><span class="n">threadID</span> <span class="o">=</span> <span class="n">threadID</span>
</span><span class='line'>        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
</span><span class='line'>        <span class="bp">self</span><span class="o">.</span><span class="n">counter</span> <span class="o">=</span> <span class="n">counter</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span><span class="c">#线程的run函数</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">print</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s"> running&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
</span><span class='line'>       <span class="c"># 获得锁，成功获得锁定后返回True</span>
</span><span class='line'>       <span class="c"># 可选的timeout参数不填时将一直阻塞直到获得锁定</span>
</span><span class='line'>       <span class="c"># 否则超时后将返回False</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">threadLock</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
</span><span class='line'>        <span class="n">showThreadMessage</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">counter</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
</span><span class='line'>        <span class="c"># 释放锁</span>
</span><span class='line'>        <span class="n">threadLock</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">showThreadMessage</span><span class="p">(</span><span class="n">threadName</span><span class="p">,</span> <span class="n">delay</span><span class="p">,</span> <span class="n">counter</span><span class="p">):</span>
</span><span class='line'>    <span class="k">while</span> <span class="n">counter</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
</span><span class='line'>        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">delay</span><span class="p">)</span>
</span><span class='line'>        <span class="k">print</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s"> showThreadMessage&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">threadName</span><span class="p">)</span>
</span><span class='line'>        <span class="n">counter</span> <span class="o">-=</span> <span class="mi">1</span>
</span><span class='line'>
</span><span class='line'><span class="n">threadLock</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Lock</span><span class="p">()</span>
</span><span class='line'><span class="n">threads</span> <span class="o">=</span> <span class="p">[]</span>
</span><span class='line'>
</span><span class='line'><span class="c"># 创建新线程</span>
</span><span class='line'><span class="n">thread1</span> <span class="o">=</span> <span class="n">QThread</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s">&quot;KSnow&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
</span><span class='line'><span class="n">thread2</span> <span class="o">=</span> <span class="n">QThread</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="s">&quot;KAir&quot;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="c"># 线程开始执行</span>
</span><span class='line'><span class="n">thread1</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
</span><span class='line'><span class="n">thread2</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'><span class="c"># 添加线程到线程列表</span>
</span><span class='line'><span class="n">threads</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">thread1</span><span class="p">)</span>
</span><span class='line'><span class="n">threads</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">thread2</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="c"># 等待所有线程完成</span>
</span><span class='line'><span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">threads</span><span class="p">:</span>
</span><span class='line'>    <span class="n">t</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
</span><span class='line'><span class="k">print</span> <span class="s">&quot;Exiting From Main Thread&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>运行情况如下:</p>

<pre><code>ksnowlv@ksnowlvdeMacBook-Pro~/Documents/lvwei projects/python$python thread_synchronization.py
KSnow running
KAir running
KSnow showThreadMessage
KSnow showThreadMessage
KSnow showThreadMessage
KAir showThreadMessage
KAir showThreadMessage
KAir showThreadMessage
Exiting From Main Thread
</code></pre>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/08/07/python-xian-cheng-zhi-threading/">Python线程之Threading</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-08-07T22:05:00+08:00" pubdate data-updated="true">Aug 7<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/08/07/python-xian-cheng-zhi-threading/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h4>接上文，介绍下<code>类包装线程对象方式</code>使用线程</h4>

<h4>python通过两个标准库thread和threading提供对线程的支持。thread提供了低级别的、原始的线程以及一个简单的锁。</h4>

<h4>样例会使用Threading模块创建线程，涉及到线程启动和运行</h4>

<pre><code>run(): 用以表示线程活动的方法。
start():启动线程活动。
</code></pre>

<p>我们的QThread类会从threading.Thread继承，然后重写<strong>init</strong>方法和run方法。</p>

<p>示例代码如下</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="c">#!/usr/bin/python</span>
</span><span class='line'>
</span><span class='line'><span class="kn">import</span> <span class="nn">threading</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">time</span>
</span><span class='line'>
</span><span class='line'><span class="n">exitFlag</span> <span class="o">=</span> <span class="mi">0</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">QThread</span><span class="p">(</span><span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">):</span><span class="c">#</span>
</span><span class='line'>
</span><span class='line'>  <span class="sd">&quot;&quot;&quot;QThread class&quot;&quot;&quot;</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">threadID</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">counter</span><span class="p">):</span>
</span><span class='line'>      <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
</span><span class='line'>      <span class="bp">self</span><span class="o">.</span><span class="n">__threadID</span> <span class="o">=</span> <span class="n">threadID</span>
</span><span class='line'>      <span class="bp">self</span><span class="o">.</span><span class="n">__name</span> <span class="o">=</span> <span class="n">name</span>
</span><span class='line'>      <span class="bp">self</span><span class="o">.</span><span class="n">__counter</span> <span class="o">=</span> <span class="n">counter</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span><span class="c">#线程在创建后会直接运行run函数 </span>
</span><span class='line'>      <span class="k">print</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s"> start&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__name</span><span class="p">)</span>
</span><span class='line'>      <span class="n">showMessage</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__counter</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
</span><span class='line'>      <span class="k">print</span> <span class="s">&quot;Exit from&quot;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">__name</span>
</span><span class='line'>  
</span><span class='line'>  
</span><span class='line'><span class="c">#定义函数</span>
</span><span class='line'><span class="k">def</span> <span class="nf">showMessage</span><span class="p">(</span><span class="n">threadName</span><span class="p">,</span> <span class="n">delayTime</span><span class="p">,</span> <span class="n">counter</span><span class="p">):</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">while</span> <span class="n">counter</span><span class="p">:</span>
</span><span class='line'>
</span><span class='line'>          <span class="k">if</span> <span class="n">exitFlag</span><span class="p">:</span>
</span><span class='line'>              <span class="n">thread</span><span class="o">.</span><span class="n">exit</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'>          <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">delayTime</span><span class="p">)</span>
</span><span class='line'>          <span class="k">print</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s"> : </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">threadName</span><span class="p">,</span> <span class="n">time</span><span class="o">.</span><span class="n">ctime</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="p">)</span>
</span><span class='line'>          <span class="n">counter</span> <span class="o">-=</span> <span class="mi">1</span>
</span><span class='line'>
</span><span class='line'><span class="c">#创建线程</span>
</span><span class='line'><span class="n">t1</span> <span class="o">=</span> <span class="n">QThread</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;ksnow&quot;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
</span><span class='line'><span class="n">t2</span> <span class="o">=</span> <span class="n">QThread</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="s">&quot;kair&quot;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
</span><span class='line'><span class="n">t3</span> <span class="o">=</span> <span class="n">QThread</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="s">&quot;baby&quot;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="c">#开启线程</span>
</span><span class='line'><span class="n">t1</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
</span><span class='line'><span class="n">t2</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
</span><span class='line'><span class="n">t3</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'><span class="k">print</span> <span class="s">&quot;exit from Main Thread&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>运行结果如下：</p>

<pre><code>ksnowlv@ksnowlvdeMacBook-Pro~/Documents/lvwei projects/python$python python_threading.py 
ksnow start
kair start
baby start
exit from Main Thread
ksnow : Thu Aug  7 22:41:42 2014
kair : Thu Aug  7 22:41:43 2014
baby : Thu Aug  7 22:41:43 2014
ksnow : Thu Aug  7 22:41:44 2014
kair : Thu Aug  7 22:41:46 2014
baby : Thu Aug  7 22:41:46 2014
ksnow : Thu Aug  7 22:41:46 2014
ksnow : Thu Aug  7 22:41:48 2014
kair : Thu Aug  7 22:41:49 2014
baby : Thu Aug  7 22:41:49 2014
ksnow : Thu Aug  7 22:41:50 2014
kair : Thu Aug  7 22:41:52 2014
baby : Thu Aug  7 22:41:52 2014
ksnow : Thu Aug  7 22:41:52 2014
Exit fromksnow
baby : Thu Aug  7 22:41:55 2014
 kair : Thu Aug  7 22:41:55 2014
baby : Thu Aug  7 22:41:58 2014
 Exit frombaby
kair : Thu Aug  7 22:41:58 2014
Exit fromkair
</code></pre>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/08/07/python-xian-cheng-zhi-start-new-thread/">Python线程之start_new_thread</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-08-07T21:13:00+08:00" pubdate data-updated="true">Aug 7<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/08/07/python-xian-cheng-zhi-start-new-thread/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h4>Python中使用线程有两种方式：</h4>

<pre><code>1.函数式方式
2.类包装线程对象方式。
</code></pre>

<p>下面使用<code>函数式方式</code>，调用thread模块中的start_new_thread()函数来产生新线程。</p>

<p>语法格式如下:</p>

<pre><code>thread.start_new_thread ( function, args[, kwargs] )
</code></pre>

<p>代码示例如下:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="c">#!/usr/bin/python</span>
</span><span class='line'>
</span><span class='line'><span class="kn">import</span> <span class="nn">thread</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">time</span>
</span><span class='line'>
</span><span class='line'><span class="c">#为线程定义一个函数</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span>  <span class="nf">showTime</span><span class="p">(</span><span class="n">threadName</span><span class="p">,</span><span class="n">delayTime</span><span class="p">):</span>
</span><span class='line'>  <span class="n">countTime</span> <span class="o">=</span> <span class="mi">0</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">while</span> <span class="n">countTime</span> <span class="o">&lt;</span> <span class="mi">5</span><span class="p">:</span>
</span><span class='line'>      <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">delayTime</span><span class="p">)</span>
</span><span class='line'>      <span class="n">countTime</span> <span class="o">+=</span> <span class="mi">1</span>
</span><span class='line'>      <span class="k">print</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s"> : </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">threadName</span><span class="p">,</span> <span class="n">time</span><span class="o">.</span><span class="n">ctime</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="p">)</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="c">#创建线程</span>
</span><span class='line'><span class="k">try</span><span class="p">:</span>
</span><span class='line'>  <span class="n">thread</span><span class="o">.</span><span class="n">start_new_thread</span> <span class="p">(</span><span class="n">showTime</span><span class="p">,</span> <span class="p">(</span><span class="s">&quot;ksnow thread&quot;</span><span class="p">,</span> <span class="mi">2</span> <span class="p">)</span> <span class="p">)</span>
</span><span class='line'>  <span class="n">thread</span><span class="o">.</span><span class="n">start_new_thread</span> <span class="p">(</span><span class="n">showTime</span><span class="p">,</span> <span class="p">(</span><span class="s">&quot;kair thread&quot;</span><span class="p">,</span> <span class="mi">3</span> <span class="p">)</span> <span class="p">)</span>
</span><span class='line'>  <span class="n">thread</span><span class="o">.</span><span class="n">start_new_thread</span> <span class="p">(</span><span class="n">showTime</span><span class="p">,</span> <span class="p">(</span><span class="s">&quot;baby thread&quot;</span><span class="p">,</span> <span class="mi">5</span> <span class="p">)</span> <span class="p">)</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="k">except</span><span class="p">:</span>
</span><span class='line'>  <span class="k">print</span> <span class="s">&quot;Error: unable to start thread&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="k">while</span> <span class="mi">1</span><span class="p">:</span>
</span><span class='line'>   <span class="k">pass</span>
</span><span class='line'>
</span></code></pre></td></tr></table></div></figure>


<p>运行结果如下：</p>

<pre><code>ksnowlv@ksnowlvdeMacBook-Pro~/Documents/lvwei projects/python$python thread_start_new_thread.py 
ksnow thread : Thu Aug  7 21:11:33 2014
kair thread : Thu Aug  7 21:11:34 2014
ksnow thread : Thu Aug  7 21:11:35 2014
baby thread : Thu Aug  7 21:11:36 2014
kair thread : Thu Aug  7 21:11:37 2014
ksnow thread : Thu Aug  7 21:11:37 2014
ksnow thread : Thu Aug  7 21:11:39 2014
kair thread : Thu Aug  7 21:11:40 2014
baby thread : Thu Aug  7 21:11:41 2014
ksnow thread : Thu Aug  7 21:11:41 2014
kair thread : Thu Aug  7 21:11:43 2014
baby thread : Thu Aug  7 21:11:46 2014
kair thread : Thu Aug  7 21:11:46 2014
baby thread : Thu Aug  7 21:11:51 2014
baby thread : Thu Aug  7 21:11:56 2014
</code></pre>

<h4>注意事项</h4>

<pre><code>1.线程的结束一般依靠线程函数的自然结束；
2.可以在线程函数中调用thread.exit()，
抛出SystemExit exception，达到退出线程的目的。
</code></pre>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/08/07/python-mian-xiang-dui-xiang-han-shu-zhong-zai/">Python面向对象函数重载</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-08-07T13:40:00+08:00" pubdate data-updated="true">Aug 7<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/08/07/python-mian-xiang-dui-xiang-han-shu-zhong-zai/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h4>python支持函数重载，和C++/Objective-c/Java类似。</h4>

<pre><code>适用场景:父类的方法不能满足你的需求.
</code></pre>

<p>代码示例如下：</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="c">#！/usr/bin/python</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">QBaseClass</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
</span><span class='line'>  <span class="sd">&quot;&quot;&quot;QBaseClass class&quot;&quot;&quot;</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
</span><span class='line'>      <span class="nb">super</span><span class="p">(</span><span class="n">QBaseClass</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">()</span>
</span><span class='line'>      <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">showMessage</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class='line'>      <span class="k">print</span> <span class="s">&quot;className = </span><span class="si">%s</span><span class="s">&quot;</span>  <span class="o">%</span> <span class="p">(</span><span class="n">QBaseClass</span><span class="o">.</span><span class="n">__name__</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">QPerson</span><span class="p">(</span><span class="n">QBaseClass</span><span class="p">):</span>
</span><span class='line'>  <span class="sd">&quot;&quot;&quot;QPerson class&quot;&quot;&quot;</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span><span class="n">address</span><span class="p">):</span>
</span><span class='line'>      <span class="nb">super</span><span class="p">(</span><span class="n">QPerson</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
</span><span class='line'>      <span class="bp">self</span><span class="o">.</span><span class="n">address</span> <span class="o">=</span> <span class="n">address</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">showMessage</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class='line'>      <span class="k">print</span> <span class="s">&quot;className = </span><span class="si">%s</span><span class="s">, address = </span><span class="si">%s</span><span class="s"> &quot;</span>  <span class="o">%</span> <span class="p">(</span><span class="n">QPerson</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">address</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="n">p1</span> <span class="o">=</span> <span class="n">QPerson</span><span class="p">(</span><span class="s">&quot;ksnowlv&quot;</span><span class="p">,</span><span class="s">&quot;北京市朝阳区&quot;</span><span class="p">)</span>
</span><span class='line'><span class="n">p1</span><span class="o">.</span><span class="n">showMessage</span><span class="p">()</span>
</span><span class='line'><span class="c">#下面是调用p1父类的同名方法，看起来是不是和Objective-c类似</span>
</span><span class='line'><span class="nb">super</span><span class="p">(</span><span class="n">QPerson</span><span class="p">,</span><span class="n">p1</span><span class="p">)</span><span class="o">.</span><span class="n">showMessage</span><span class="p">()</span>
</span><span class='line'><span class="n">QBaseClass</span><span class="o">.</span><span class="n">showMessage</span><span class="p">(</span><span class="n">p1</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>运行结果如下:</p>

<pre><code>ksnowlv@ksnowlvdeMacBook-Pro~/Documents/lvwei projects/python$python overloadmethod.py 
className = QPerson, address = 北京市朝阳区
className = QBaseClass
className = QBaseClass
</code></pre>

<h4>下面两种写法虽然执行结果相同，那有什么不同呢？</h4>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="nb">super</span><span class="p">(</span><span class="n">QPerson</span><span class="p">,</span><span class="n">p1</span><span class="p">)</span><span class="o">.</span><span class="n">showMessage</span><span class="p">()</span>
</span><span class='line'><span class="n">QBaseClass</span><span class="o">.</span><span class="n">showMessage</span><span class="p">(</span><span class="n">p1</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>

</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/08/07/ji-yu-octopress-de-github-bo-ke-shi-yong-gitguan-li-yuan-dai-ma-he-wang-ye-dai-ma/">基于octopress的github博客使用git管理源代码和网页代码</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-08-07T09:12:00+08:00" pubdate data-updated="true">Aug 7<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/08/07/ji-yu-octopress-de-github-bo-ke-shi-yong-gitguan-li-yuan-dai-ma-he-wang-ye-dai-ma/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h4>基于octopress的github/gitcafe博客，都会涉及到源代码管理。对于git,github/gitcafe的初学者而言，可能容易走弯路。</h4>

<p>先看下博客目录:
 <img src="/images/post/2014-08-07-ji-yu-octopress-de-github-bo-ke-shi-yong-gitguan-li-yuan-dai-ma-he-wang-ye-dai-ma/github-blog-dir.png" alt="image" /></p>

<pre><code>lvweigithubblog和_deploy两个目录下，是不同的git版本库，对应不同分支。一定不能搞乱。
在lvweigithubblog目录下,操作目录始终是在lvweigithubblog目录:
使用git命令操作远程source分支;
使用rake generate/deploy/new_post/...操作的是远程master分支。
</code></pre>

<h4>1.静态网页代码版本管理</h4>

<h4>网页生成，rake generate 是生成本地博客pulic目录下静态网页, rake deploy 是把public目录下的静态网页文件拷贝到<em>deploy目录下，并自动调用git命令把</em>deploy目录下网页文件上传到远程分支master.</h4>

<pre><code>_deploy下的git版本库，管理的是是网页文件，操作的是master分支
</code></pre>

<p>详情参考下图:</p>

<p> <img src="/images/post/2014-08-07-ji-yu-octopress-de-github-bo-ke-shi-yong-gitguan-li-yuan-dai-ma-he-wang-ye-dai-ma/git-for-master-github-pages.png" alt="image" /></p>

<h4>2.网页源代码版本管理</h4>

<h4>lvweigithubblog下的源文件管理，可以通过git相关命令，也可以使用sourcetree等工具进行管理。可以通过<code>git status</code>查看当前所在分支</h4>

<pre><code>lvweigithubblog根目录下的git版本库实际上只管理除了public,_deploy两个主要目录以外所有的文件，octopress的配置自动忽略这两个目录。只处理这两个目录以外的版本变化情况。

该git版本库处理的是远程source分支，你可以随便命名该分支，比如a都行，只要不和master分支重名即可。所以，这个git版本库必须切换到source分支，一定不能是master分支。
</code></pre>

<p>详情参考下图:</p>

<p> <img src="/images/post/2014-08-07-ji-yu-octopress-de-github-bo-ke-shi-yong-gitguan-li-yuan-dai-ma-he-wang-ye-dai-ma/git-for-source-code.png" alt="image" /></p>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/08/06/python-mian-xiang-dui-xiang-ji-cheng-pai-sheng/">Python面向对象-继承派生</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-08-06T23:24:00+08:00" pubdate data-updated="true">Aug 6<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/08/06/python-mian-xiang-dui-xiang-ji-cheng-pai-sheng/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h4>python是支持类的继承派生，通过这种继承派生机制，我们可以实现代码重用。其实，与C++/Objective-c/Java类似。</h4>

<h4>语法格式如下:</h4>

<pre><code>class 派生类名（基类名):
</code></pre>

<h4>python继承中的一些特点:</h4>

<h5>1.在继承中基类的构造（<strong>init</strong>()方法）<code>不会被自动调用</code>，它需要在其派生类的构造中手动调用。</h5>

<h5>2：在调用基类的方法时，需要加上基类的类名前缀，且需要带上self参数变量。区别于在类中调用普通函数时并不需要带上self参数</h5>

<h5>3：Python总是首先查找对应类型的方法，如果它不能在派生类中找到对应的方法，它才开始到基类中逐个查找。（先在本类中查找调用的方法，找不到才去基类中找）。</h5>

<h4>如果派生类继承的基类超过一个，那么该派生类就被称作&#8221;多重继承&#8221;.</h4>

<h4>语法格式如下：</h4>

<pre><code>class 派生类名（基类名1，基类2，...,基类n):
</code></pre>

<p>代码示例如下:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="c">#!/usr/bin/python</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">QBaseObject</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="c">#define base class</span>
</span><span class='line'>
</span><span class='line'>  <span class="s">&quot;base class&quot;</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">value</span> <span class="o">=</span> <span class="mi">0</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class='line'>      <span class="k">print</span> <span class="s">&quot;QBaseObject no param&quot;</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">name</span><span class="p">):</span>
</span><span class='line'>      <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
</span><span class='line'>      <span class="k">print</span> <span class="s">&quot;QBaseObject param name:&quot;</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">setName</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">name</span><span class="p">):</span>
</span><span class='line'>      <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">getName</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class='line'>      <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">;</span>           
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">QPerson</span><span class="p">(</span><span class="n">QBaseObject</span><span class="p">):</span>
</span><span class='line'>  <span class="sd">&quot;&quot;&quot;QPerson class&quot;&quot;&quot;</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">address</span><span class="p">):</span>
</span><span class='line'>      <span class="nb">super</span><span class="p">(</span><span class="n">QPerson</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">()</span>
</span><span class='line'>      <span class="bp">self</span><span class="o">.</span><span class="n">address</span> <span class="o">=</span> <span class="n">address</span>
</span><span class='line'>      <span class="k">print</span> <span class="s">&quot;QPerson address&quot;</span>
</span><span class='line'>
</span><span class='line'>  
</span><span class='line'>  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span><span class="n">address</span><span class="p">):</span>
</span><span class='line'>      <span class="nb">super</span><span class="p">(</span><span class="n">QPerson</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
</span><span class='line'>      <span class="bp">self</span><span class="o">.</span><span class="n">address</span> <span class="o">=</span> <span class="n">address</span>
</span><span class='line'>      <span class="k">print</span> <span class="s">&quot;QPerson name &amp;address&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="c">#p1 = QPerson(&quot;北京市朝阳区&quot;)</span>
</span><span class='line'><span class="c">#print &quot;p1 name = %s, address = %s value = %d&quot; % (p1.name,p1.address,QPerson.value)</span>
</span><span class='line'>
</span><span class='line'><span class="n">p2</span> <span class="o">=</span> <span class="n">QPerson</span><span class="p">(</span><span class="s">&quot;ksnowlv&quot;</span><span class="p">,</span><span class="s">&quot;北京市朝阳区&quot;</span><span class="p">)</span>
</span><span class='line'><span class="n">QPerson</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="mi">1</span>
</span><span class='line'><span class="k">print</span> <span class="s">&quot;p2 name = </span><span class="si">%s</span><span class="s">, address = </span><span class="si">%s</span><span class="s"> value = </span><span class="si">%d</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">p2</span><span class="o">.</span><span class="n">name</span><span class="p">,</span><span class="n">p2</span><span class="o">.</span><span class="n">address</span><span class="p">,</span><span class="n">QPerson</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="n">p2</span><span class="o">.</span><span class="n">setName</span><span class="p">(</span><span class="s">&quot;kAir&quot;</span><span class="p">)</span>
</span><span class='line'><span class="n">QPerson</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="mi">2</span>
</span><span class='line'><span class="k">print</span> <span class="s">&quot;p1 name = </span><span class="si">%s</span><span class="s">, address = </span><span class="si">%s</span><span class="s"> value = </span><span class="si">%d</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">p2</span><span class="o">.</span><span class="n">name</span><span class="p">,</span><span class="n">p2</span><span class="o">.</span><span class="n">address</span><span class="p">,</span><span class="n">QPerson</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="n">p2</span><span class="o">.</span><span class="n">setName</span><span class="p">(</span><span class="s">&quot;ksnowlv&quot;</span><span class="p">)</span>
</span><span class='line'><span class="k">print</span> <span class="s">&quot;p2 名字还原为&quot;</span><span class="p">,</span><span class="n">p2</span><span class="o">.</span><span class="n">getName</span><span class="p">()</span>
</span><span class='line'>  
</span></code></pre></td></tr></table></div></figure>


<p>运行结果如下：</p>

<pre><code>ksnowlv@ksnowlvdeMacBook-Pro~/Documents/lvwei projects/python$
python object_Inheritance.py 
QBaseObject param name: ksnowlv
QPerson name &amp;address
p2 name = ksnowlv, address = 北京市朝阳区 value = 1
p1 name = kAir, address = 北京市朝阳区 value = 2
p2 名字还原为 ksnowlv
</code></pre>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2014/08/06/python-mian-xiang-dui-xiang-python-dui-xiang-xiao-hui/">Python面向对象-python对象销毁</a></h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-08-06T21:04:00+08:00" pubdate data-updated="true">Aug 6<span>th</span>, 2014</time>
        
         | <a href="/blog/2014/08/06/python-mian-xiang-dui-xiang-python-dui-xiang-xiao-hui/#disqus_thread">Comments</a>
        
      </p>
    
  </header>


  <div class="entry-content"><h4>同Java/Objective-c语言一样，Python使用了引用计数这一简单技术来追踪内存中的对象。</h4>

<h4>在Python内部记录着所有使用中的对象各有多少引用。一个内部跟踪变量，称为一个引用计数器。</h4>

<h4>当对象被创建时， 就创建了一个引用计数， 当这个对象不再需要时， 也就是说， 这个对象的引用计数变为0 时， 它被垃圾回收。但是回收不是&#8221;立即&#8221;的， 由解释器在适当的时机，将垃圾对象占用的内存空间回收。</h4>

<pre><code>a = 1 #创建对象a
b = a #对象a引用计数+1 
c = a #对象a引用计数+1

del a #对象a引用计数-1
b = 2 #对象a引用计数-1
c = 2 #对象a引用计数-1，此时对象a引用计数为0，可能会被回收
</code></pre>

<h4>垃圾回收机制不仅针对引用计数为0的对象;也可以处理循环引用的情况。</h4>

<pre><code>循环引用指的是，两个对象相互引用，但是没有其他变量引用他们。
这种情况下，仅使用引用计数是不够的。
Python 的垃圾收集器实际上是一个引用计数器和一个循环垃圾收集器。
作为引用计数的补充， 垃圾收集器也会留心被分配的总量很大（及未通过引用计数销毁的那些）的对象。 
在这种情况下， 解释器会暂停下来， 试图清理所有未引用的循环。
</code></pre>

<p>代码示例如下：</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="c">#!/usr/bin/python</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">QPoint</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
</span><span class='line'>  <span class="s">&quot;point for QPoint&quot;</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">z</span> <span class="o">=</span> <span class="mi">0</span><span class="p">):</span>
</span><span class='line'>      <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span>
</span><span class='line'>      <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">y</span>
</span><span class='line'>      <span class="bp">self</span><span class="o">.</span><span class="n">z</span> <span class="o">=</span> <span class="n">z</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">__del__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class='line'>          <span class="n">class_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span>
</span><span class='line'>          <span class="k">print</span> <span class="n">class_name</span><span class="p">,</span><span class="s">&quot;destroyed&quot;</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="n">point1</span> <span class="o">=</span> <span class="n">QPoint</span><span class="p">()</span>
</span><span class='line'><span class="n">point2</span> <span class="o">=</span> <span class="n">point1</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="n">point3</span> <span class="o">=</span> <span class="n">QPoint</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="k">print</span> <span class="nb">id</span><span class="p">(</span><span class="n">point1</span><span class="p">),</span> <span class="nb">id</span><span class="p">(</span><span class="n">point2</span><span class="p">),</span> <span class="nb">id</span><span class="p">(</span><span class="n">point3</span><span class="p">)</span> <span class="c">#输出对象的id</span>
</span><span class='line'>
</span><span class='line'><span class="c">#del point1</span>
</span><span class='line'><span class="c">#del point2</span>
</span><span class='line'><span class="c">#del point3</span>
</span></code></pre></td></tr></table></div></figure>


<p>运行结果如下：</p>

<pre><code>ksnowlv@ksnowlvdeMacBook-Pro~/Documents/lvwei projects/python$python object_destroy.py 
4324792912 4324792912 4324954192
QPoint destroyed
QPoint destroyed
</code></pre>
</div>
  
  


    </article>
  
  <div class="pagination">
    
      <a class="prev" href="/blog/page/12/">&larr; Older</a>
    
    <a href="/blog/archives">Blog Archives</a>
    
    <a class="next" href="/blog/page/10/">Newer &rarr;</a>
    
  </div>
</div>
<aside class="sidebar">
  
    <section>
  <h1>关于我</h1>
  <p>律威,QQ:309235009 微信:ksnowlv</p>
  <p>手机单机游戏/手机网游封神online/手机阅读/腾讯iOS地图客户端&实时公交&开放API(地图&街景)/iPad/iPhone淘宝客户端及性能优化</p>
  <p>目前在银客做银客/简理财/&#8230;</p>
  <p>能做一点,是一点;不求大成,不求一万年;积少成多,水到渠成!</p>
</section>
<section>
  <h1>最近发布</h1>
  <ul id="recent_posts">
    
      <li class="post">
        <a href="/blog/2017/04/26/android-drawable/">Android drawable</a>
      </li>
    
      <li class="post">
        <a href="/blog/2017/04/22/iOS-xia-shi-yong-ffmpeg/">iOS下使用ffmpeg</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/08/19/ios-you-xian-hou-tai-ren-wu/">iOS有限后台任务</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/08/19/android-xia-jie-ya-suo/">Android下gzip数据流解压缩</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/08/19/facebook-infer-de-shi-yong/">FaceBook-infer的使用</a>
      </li>
    
  </ul>
</section>
<section>
  <h1>分类</h1>
    <ul id="category-list"><li><a href='/blog/categories/android-jie-ya-suo/'>Android-解压缩 (1)</a></li><li><a href='/blog/categories/androidduo-xian-cheng/'>Android多线程 (3)</a></li><li><a href='/blog/categories/androidcha-jian/'>Android插件 (1)</a></li><li><a href='/blog/categories/androidhui-tu-./'>Android绘图。 (1)</a></li><li><a href='/blog/categories/androidwang-luo/'>Android网络 (1)</a></li><li><a href='/blog/categories/git/'>git (6)</a></li><li><a href='/blog/categories/githubbo-ke/'>github博客 (8)</a></li><li><a href='/blog/categories/ios/'>iOS (6)</a></li><li><a href='/blog/categories/ios-block/'>iOS-block (1)</a></li><li><a href='/blog/categories/ios-bug/'>iOS-Bug (2)</a></li><li><a href='/blog/categories/ios-memory/'>iOS-Memory (4)</a></li><li><a href='/blog/categories/ios-push/'>iOS-push (1)</a></li><li><a href='/blog/categories/ios-ui/'>iOS-UI (2)</a></li><li><a href='/blog/categories/ios-you-hua/'>iOS-优化 (1)</a></li><li><a href='/blog/categories/ios-nei-cun/'>iOS-内存 (1)</a></li><li><a href='/blog/categories/ios-ji-chu/'>iOS-基础 (3)</a></li><li><a href='/blog/categories/ios-duo-xian-cheng/'>iOS-多线程 (3)</a></li><li><a href='/blog/categories/ios-cun-chu/'>iOS-存储 (1)</a></li><li><a href='/blog/categories/ios-an-quan/'>iOS-安全 (1)</a></li><li><a href='/blog/categories/ios-wang-luo/'>iOS-网络 (13)</a></li><li><a href='/blog/categories/ios-jie-ya-suo/'>iOS-解压缩 (1)</a></li><li><a href='/blog/categories/ios-she-ji-mo-shi/'>iOS-设计模式 (1)</a></li><li><a href='/blog/categories/ios-diao-shi/'>iOS-调试 (1)</a></li><li><a href='/blog/categories/iosyou-hua/'>iOS优化 (1)</a></li><li><a href='/blog/categories/iosji-chu/'>iOS基础 (2)</a></li><li><a href='/blog/categories/iosyin-pin/'>iOS音频 (1)</a></li><li><a href='/blog/categories/mac/'>Mac (4)</a></li><li><a href='/blog/categories/mac-duo-xian-cheng/'>Mac-多线程 (1)</a></li><li><a href='/blog/categories/nginx/'>nginx (3)</a></li><li><a href='/blog/categories/php/'>php (3)</a></li><li><a href='/blog/categories/python/'>python (12)</a></li><li><a href='/blog/categories/swift/'>swift (11)</a></li><li><a href='/blog/categories/uml/'>UML (8)</a></li><li><a href='/blog/categories/xcode/'>Xcode (11)</a></li><li><a href='/blog/categories/chan-pin-she-ji/'>产品设计 (2)</a></li><li><a href='/blog/categories/gong-ju/'>工具 (2)</a></li><li><a href='/blog/categories/shu-ju-ku/'>数据库 (2)</a></li><li><a href='/blog/categories/sheng-huo-pian-duan/'>生活片段 (10)</a></li><li><a href='/blog/categories/zhi-ye-sheng-ya/'>职业生涯 (7)</a></li><li><a href='/blog/categories/du-shu/'>读书 (2)</a></li></ul>
</section>
<section>
  <h1>外部链接</h1>
    <ul id="外部链接">
    	<li><a href="https://developer.apple.com">苹果开发者中心</a> </li>
    	<li><a href="http://stackoverflow.com/questions/tagged/ios">stackoverflow</a></li>
    	<li><a href="http://ksnowlv.blog.163.com/blog/#m=0">ksnowlv网易博客</a></li>
	</ul>
</section>

  
</aside>

    </div>
  </div>
  <footer role="contentinfo"><p>
  Copyright &copy; 2017 - 律威 -
  <span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>

  <script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1253538110'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "v1.cnzz.com/z_stat.php%3Fid%3D1253538110%26online%3D1' type='text/javascript'%3E%3C/script%3E"));</script>

  <script type="text/javascript">
var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3F4c1727a94ea20751730e8a556dbdbf82' type='text/javascript'%3E%3C/script%3E"));
</script>

</p>



</footer>
  

<script type="text/javascript">
      var disqus_shortname = 'ksnowlv';
      
        
        var disqus_script = 'count.js';
      
    (function () {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = 'http://' + disqus_shortname + '.disqus.com/' + disqus_script;
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    }());
</script>








</body>
</html>
